<template>
    <div class="login" clearfix>
        <el-row type="flex" justify="center">
            <el-form ref="loginForm" :model="user" status-icon label-width="80px" :rules="rules">
                <h3>登录</h3>
                <hr>
                <el-form-item prop="id" label="用户id">
                    <el-input v-model="user.id" placeholder="输入用户id" prefix-icon></el-input>
                </el-form-item>
                <el-form-item id="password" prop="password" label="密码">
                    <el-input v-model="user.password" show-password placeholder="输入密码"></el-input>
                </el-form-item>
                <el-form-item id="role" prop="role" label="权限">
                    <el-select v-model="user.role" placeholder="输入权限" prop="role">
                        <el-option
                            v-for="item in options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-upload" @click="doLogin()">登 录</el-button>
                </el-form-item>
            </el-form>
        </el-row>
    </div>
</template>

<script>
//Export default 是ES6的语法，它允许你在.vue文件中导出一个对象，这个对象通常是一个Vue组件。
// 这样做的目的是为了能够在其他文件中通过import语句引入并使用这个组件。
export default {
    name: "login",
    components: {},
    data() {
        return {
            uid: '123',
            uname: 'jackknives',
            user: {
                id: "",
                password: "",
                role: ""
            },
            options: [{
                value: '1',
                label: '管理员'
            }, {
                value: '2',
                label: '员工'
            }]
        };
    },
    computed: {},
    watch: {},
    methods: {
        doLogin() {
            if (!this.user.id) {
                this.$message.error("请输入用户名！");
                return;
            }
            if (!this.user.password) {
                this.$message.error("请输入密码！");
                return;
            }
            this.$http.post("user/login", this.user).then(res => {
                debugger
                if (res.data.code === "200") {
                    //将用户名存储到vuex中
                    //调用vuex提供的actions方法实现数据存储，推荐

                    this.$store.dispatch("setUid", res.data.user.id)
                    this.getName()

                    //直接调用mutations中的方法，不推荐
                    //this.$store.commit("setUname",this.loginForm.uname)

                    //将用户名存储到sessionStorage中
                    // sessionStorage.setItem("uname", res.data.user.name);
                    // sessionStorage.setItem("uid", this.user.uid)

                    //验证通过，跳转页面
                    if (this.user.role === "1") {
                        this.$router.push({
                            path: '/admin'
                        })
                    } else {
                        this.$router.push({
                            path: '/employee'
                        })
                    }

                } else {
                    this.$message.error('用户名或者密码错误');
                }
            })
        },
        getName() {
            let empno = this.$store.getters.getUid
            this.$http.get("/employee/getEmp/" + empno).then(res => {
                this.$store.dispatch("setUname", res.data.name)
            })
        },
        rules: {
            id: [{
                required: true, message: "输入员工姓名", trigger: "blur"
            }],
            password: [{
                required: true, message: "输入密码", trigger: "blur"
            }],
            role: [{
                required: true, message: "输入权限", trigger: "blur"
            }]
        },
    },
    beforeCreate() {
    },

    created() {

    },

    beforeMount() {

    },

    mounted() {

    },

    beforeUpdate() {

    },

    updated() {

    },

    beforeDestroy() {

    },
    destroyed() {

    },

    activated() {

    },
}
</script>

<style scoped>
.login {
    width: 100%;
    height: 740px;
    /* background: url("../assets/images/bg1.png") no-repeat;*/
    background-size: cover;
    overflow: hidden;
}
#password {
    margin-bottom: 5px;
}

h3 {
    color: #0babeab8;
    font-size: 24px;
}

hr {
    background-color: #444;
    margin: 20px auto;
}

a {
    text-decoration: none;
    color: #aaa;
    font-size: 15px;
}

a:hover {
    color: coral;
}

</style>
